package com.disys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.disys.facade.PersonFacade;
import com.disys.model.Person;
import com.sun.faces.context.flash.ELFlash;

@ViewScoped
@ManagedBean
public class PersonMB extends AbstractMB implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final String SELECTED_PERSON = "selectedPerson";

	private Person person;
	private Person personWithDogs;
	private Person personWithDogsForDetail;

	private List<Person> persons;

	private PersonFacade personFacade;

	public void createPerson() {
		try {
			getPersonFacade().createPerson(person);
			closeDialog();
			displayInfoMessageToUser("Created With Sucess");
			loadPersons();
			resetPerson();
		} catch (Exception e) {
			keepDialogOpen();
			displayErrorMessageToUser("Ops, we could not create. Try again later");
			e.printStackTrace();
		}
	}

	public void updatePerson() {
		try {
			getPersonFacade().updatePerson(person);
			closeDialog();
			displayInfoMessageToUser("Updated With Sucess");
			loadPersons();
			resetPerson();
		} catch (Exception e) {
			keepDialogOpen();
			displayErrorMessageToUser("Ops, we could not create. Try again later");
			e.printStackTrace();
		}
	}

//	public void deletePerson() {
//		try {
//			getPersonFacade().deletePerson(person);
//			closeDialog();
//			displayInfoMessageToUser("Deleted With Sucess");
//			loadPersons();
//			resetPerson();
//		} catch (Exception e) {
//			keepDialogOpen();
//			displayErrorMessageToUser("Ops, we could not create. Try again later");
//			e.printStackTrace();
//		}
//	}

	
//	public Person getPersonWithDogs() {
//		if (personWithDogs == null) {
//			person = (Person) ELFlash.getFlash().get(SELECTED_PERSON);
//			personWithDogs = getPersonFacade().findPersonWithAllDogs(person.getId());
//		}
//
//		return personWithDogs;
//	}
//
//	public void setPersonWithDogsForDetail(Person person) {
//		personWithDogsForDetail = getPersonFacade().findPersonWithAllDogs(person.getId());
//	}

	
	
	
	public PersonFacade getPersonFacade() {
		if (personFacade == null) {
			personFacade = new PersonFacade();
		}

		return personFacade;
	}

	public Person getPerson() {
		if (person == null) {
			person = new Person();
		}

		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public List<Person> getAllPersons() {
		if (persons == null) {
			loadPersons();
		}

		return persons;
	}

	private void loadPersons() {
		persons = getPersonFacade().listAll();
	}

	public void resetPerson() {
		person = new Person();
	}

	
}